/**
 * @Copyright:Copyright (c) 2007 - 2023
 * 
 */
package com.tengtong.duang.app.query.core;

import java.util.Arrays;
import java.util.List;

import com.tengtong.commons.collections.ListUtils;
import com.tengtong.commons.lang.StringUtils;

/**
 * 列字段
 *
 * @author lin_jinming
 * @since: v1.0
 */
public class ColumnsBuilder {

    private final FieldDelimiter fieldDelimiter;
    
    private List<String> columns = ListUtils.newList();
    
    public ColumnsBuilder() {
        this.fieldDelimiter = FieldDelimiter.DEFAULT;
    }
    
    public ColumnsBuilder(final FieldDelimiter fd) {
        this.fieldDelimiter = fd;
    }
    
    /**
     * 添加字段
     *
     * @param fields
     * @return
     */
    public ColumnsBuilder addColumns(String...fields) {
        Arrays.stream(fields)
              .filter(StringUtils::notEmpty)
              .forEach(f->columns.add(fieldDelimiter.fieldQuote(f.trim())));
        return this;
    }
    
    public ColumnsBuilder addColumn(String field) {
        if(StringUtils.notEmpty(field)) {
            columns.add(fieldDelimiter.fieldQuote(field.trim()));
        }
        return this;
    }
    
    public String toColumns() {
        if(ListUtils.isEmpty(columns)) {
            return this.fieldDelimiter.fieldQuote("*");
        }
        return StringUtils.join(columns, ",");
    }
}
